package com.coffee.controller;

import com.coffee.dto.CoffeeDto;
import com.coffee.entity.*;
import com.coffee.service.CategoryService;
import com.coffee.service.CoffeeFlavorService;
import com.coffee.service.CoffeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

@RestController
@RequestMapping("/coffee")
public class CoffeeController {
    @Autowired
    private CoffeeService coffeeService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CoffeeFlavorService coffeeFlavorService;

    /**
     * 手机端根据分类id查询咖啡列表
     *
     * @param coffee
     * @return
     */
    @GetMapping("/list")
    public R list(Coffee coffee) {
        //根据分类id查询咖啡分类
        List<Coffee> list = coffeeService.list(coffee.getCategoryId());
        List<CoffeeDto> coffeeDtoList = new ArrayList<>();
        for (Coffee item : list) {
            CoffeeDto coffeeDto = new CoffeeDto();
            //拷贝咖啡信息
            BeanUtils.copyProperties(item, coffeeDto);
            //根据分类id查询分类名称
            Category category = categoryService.getById(coffee.getCategoryId());
            if (category != null) {
                coffeeDto.setCategoryName(category.getName());
            }
            //根据咖啡id查询分类口味
            List<CoffeeFlavor> coffeeFlavorList = coffeeFlavorService.list(item.getId());
            coffeeDto.setFlavors(coffeeFlavorList);
            coffeeDtoList.add(coffeeDto);
        }
        return R.success(coffeeDtoList);
    }

    /**
     * pc端查询咖啡列表
     *
     * @return
     */
    @PostMapping("/page")
    public R page(@RequestBody QueryPageBean queryPageBean) {
        PageResult result = coffeeService.page(queryPageBean);
        return R.success(result);
    }

    @PostMapping("/add")
    public R add(@RequestBody CoffeeDto coffeeDto) {
        coffeeService.add(coffeeDto);
        return R.success(coffeeDto);
    }

    @PostMapping("/edit")
    public R edit(@RequestBody CoffeeDto coffeeDto) {
        coffeeService.edit(coffeeDto);
        return R.success(coffeeDto);
    }

    @PostMapping("/delete")
    public R delete(@RequestBody Coffee coffee) {
        coffeeService.delete(coffee);
        return R.success(coffee);
    }

    @PostMapping("/status")
    public R status(@RequestBody Coffee coffee) {
        coffeeService.status(coffee);
        return R.success(coffee);
    }

    /**
     * 活动咖啡展示
     * @return
     */
    @GetMapping("/disCount")
    public R disCount(){
       List<Coffee> disCountList =coffeeService.listDisCount();
        return R.success(disCountList);
    }

}
